package com.haoziqi.chapter_05;

import com.haoziqi.chapter_02.WaterSensor;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * description
 * created by A on 2021/3/4
 */
public class FlatMapTrans {
    public static void main(String[] args) {
        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.readTextFile("input").flatMap(new MyFun()).print();
        //提交任务
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static class MyFun extends RichFlatMapFunction<String,WaterSensor>{
        @Override
        public void open(Configuration parameters) throws Exception {
            System.out.println("open...");
        }

        @Override
        public void close() throws Exception {
            System.out.println("close");
        }

        public void flatMap(String value, Collector<WaterSensor> out) throws Exception {
            if(!value.equals("")){
                out.collect(new WaterSensor("1",System.currentTimeMillis(),10));
            }
        }
    }
}
